PostgreSQL pg_profile 收集子快照
1 背景知识
PostgreSQL 数据库中的某一些性能数据是不可累积的。例如,会话状态只通过 pg_stat_activity 视图提供。这一类的数据只能通过高频率进行采集才能获得。
子快照提供了一个 take_subsapmle
函数,用于收集这一部分快速变化的数据。
2 收集数据种类
- 长时间查询
- 长事务。
- 过期交易。
- 长时间的空闲会话。
3 参数和函数
关于子快照的函数和参数请见 子快照函数 和 子快照配置参数详解 两个章节。
当会话超过以下任何一个阈值时,就会把会话状态保存在存储库中。
在执行查询期间,now()
至 query_start
之间的差值超过了 pg_profile .min_query_duration 时。
在执行查询期间,now()
至 xact_start
之间的差值超过了 pg_profile .min_xact_duration 时。
在执行查询期间,now()
至 xact_age(backend_xmin)
之间的差值超过了 pg_profile.min_xact_age 时。
在执行查询期间,now()
至 state_change
之间的差值超过了
pg_profile.min_idle_xact_duration 时。
这种状态称之为空载事务。
4 子快照的采样速度
子快照采样的速度非常快,可以使用较高的采集频率进行采集。但是每分钟不要超过2-4 次。
由于 Linux crontab 只能每分钟调用一次。所以需要使用其他方式实现任务调度。
下面使用 \watch 15
命令进行任务调度。
echo "select take_subsample(); \watch 15" | psql &> /dev/null
当然也可以使用 systemd 服务进行调度。
vi /etc/systemd/system/subsample.service
[Unit]
Description=pg_profile subsampling unit
[Service]
Type=simple
ExecStart=/bin/sh -c 'echo "select profile.take_subsample(); \\watch 15" | /usr/local/pgsql/bin/psql -U postgres -d testdb -qo /dev/null'
User=postgres
Group=postgres
[Install]
WantedBy=multi-user.target
systemctl enable subsample.service
systemctl start subsample.service
Warning
如果你重启了数据库,由于连接会话中断,所以请重启 subsample.service
服务,如果不手工干预否则不会收集子块照信息。